package org.net.device.tools;

import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;

public class gridheaderstruct {
	String hidelist="";
	String selector="";
	ArrayList al = new ArrayList();
	
	public void pushheaderitem(String id,String caption,int width,String type,boolean visual,String opt) {
		JSONObject jo = new JSONObject();
		jo.put("id", id);
		jo.put("caption", caption);
		jo.put("width", width);
		jo.put("type", type);
		if (type.equalsIgnoreCase("combobox") || type.equalsIgnoreCase("listbox") || type.equalsIgnoreCase("helpinput")) {
			if (selector.equalsIgnoreCase("")) {
				selector = id;
			} else {
				selector = selector + ","+id;
			}			
		}
		if (!visual) {
			if (hidelist.equalsIgnoreCase("")) {
				hidelist = id;
			} else {
				hidelist = hidelist + ","+id;
			}
		}
		
		if (!opt.equalsIgnoreCase("")) {
			JSONObject jsonopt = JSONObject.fromObject(opt);
			String k;
			for (Iterator it = jsonopt.keys();it.hasNext();) {
				k = (String)it.next();
				jo.put(k, jsonopt.get(k));
			}
		}
		al.add(jo.toString());		
	}
	
	public String getHeaderStructs() {
		String headers = "";
		for (int i = 0;i < al.size();i++) {
			if (headers.equalsIgnoreCase("")) {
				headers = (String)al.get(i);
			} else {
				headers = headers +","+(String)al.get(i);
			}			
		}
		if (headers.lastIndexOf(",") == headers.length()-1) {
			headers = headers.substring(0, headers.length()-1);
		}
		headers = "["+headers+"]";
		JSONObject jo = new JSONObject();
		jo.put("struct",headers);
		jo.put("hidelist", this.hidelist);
		jo.put("selectorlist", this.selector);
		return jo.toString();
	}
	 
}
